/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hisense.settings.threed;

import com.android.settings.R;
import com.hisense.settings.util.Wrapper;

import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.os.Bundle;
import android.os.Handler;
import android.os.IPowerManager;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.preference.SeekBarPreference;
import android.preference.SeekBarDialogPreference;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.SeekBar;


import android.widget.TextView;
public class ViewPointPreference extends SeekBarDialogPreference implements
        SeekBar.OnSeekBarChangeListener{
	private static final String TAG = "ViewPointPreference";
    private SeekBar mSeekBar;

    private int mOldViewPoint;

    private boolean mRestoredViewPointOldState;
	private TextView mViewPointNumView;

    // Backlight range is from 0 - 100. Need to make sure that user
    // doesn't set the backlight to 0 and get stuck
    private static final int MINIMUM_VIEW_POINT = 0;
    private static final int MAXIMUM_VIEW_POINT = 10;

    private ContentObserver mViewPointObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange) {
           //onViewPointChanged();
        }
    };


    public ViewPointPreference(Context context, AttributeSet attrs) {
        super(context, attrs);

        setDialogLayoutResource(R.layout.preference_dialog_viewpoint);
        setDialogIcon(R.drawable.ic_settings_display);
    }

    @Override
    protected void showDialog(Bundle state) {
        super.showDialog(state);

        //getContext().getContentResolver().registerContentObserver(
        //        Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS), true,
        //        mBrightnessObserver);

        mRestoredViewPointOldState = false;
    }

    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view);

        mSeekBar = (SeekBar)view.findViewById(R.id.view_point_seekbar);
        mSeekBar.setMax(MAXIMUM_VIEW_POINT - MINIMUM_VIEW_POINT);
        mOldViewPoint= getViewPoint();
        mSeekBar.setProgress(mOldViewPoint - MINIMUM_VIEW_POINT);

        mSeekBar.setOnSeekBarChangeListener(this);
		mViewPointNumView = (TextView)view.findViewById(R.id.view_point_value);
		mViewPointNumView.setText(""+mOldViewPoint);
    }

    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromTouch) {
        setViewPoint(progress + MINIMUM_VIEW_POINT);
		mViewPointNumView.setText(""+progress);
    }

    public void onStartTrackingTouch(SeekBar seekBar) {
        // NA
    }

    public void onStopTrackingTouch(SeekBar seekBar) {
        // NA
    }

    private int getViewPoint() {
        int viewpoint = 0;
   		viewpoint = Wrapper.getWrapperInstance().getInput2Dto3DAngle();
        return viewpoint;
    }


    private void onViewPointChanged() {
        int viewpoint = getViewPoint();
        mSeekBar.setProgress(viewpoint - MINIMUM_VIEW_POINT);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);

        final ContentResolver resolver = getContext().getContentResolver();
		//lca Wrapper.getWrapperInstance().setPictureMode(SCMPQSettingsEnums.SCMPictureModes.SCM_PICTURE_MODE_PRO1.ordinal());
		callChangeListener(getViewPoint());
        resolver.unregisterContentObserver(mViewPointObserver);
    }

    private void restoreViewPointOldState() {
        if (mRestoredViewPointOldState) return;

        setViewPoint(mOldViewPoint);
        mRestoredViewPointOldState = true;
    }

    private void setViewPoint(int viewpoint) {

        Wrapper.getWrapperInstance().setInput2Dto3DAngle(viewpoint);
		
    }


    @Override
    protected Parcelable onSaveInstanceState() {
        final Parcelable superState = super.onSaveInstanceState();
        if (getDialog() == null || !getDialog().isShowing()) 
			return superState;

        // Save the dialog state
        final SavedState myState = new SavedState(superState);
        myState.progress = mSeekBar.getProgress();
        myState.oldProgress = mOldViewPoint;

        // Restore the old state when the activity or dialog is being paused
        restoreViewPointOldState();
        return myState;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if (state == null || !state.getClass().equals(SavedState.class)) {
            // Didn't save state for us in onSaveInstanceState
            super.onRestoreInstanceState(state);
            return;
        }

        SavedState myState = (SavedState) state;
        super.onRestoreInstanceState(myState.getSuperState());
        mOldViewPoint= myState.oldProgress;
        setViewPoint(myState.progress + MINIMUM_VIEW_POINT);
    }

    private static class SavedState extends BaseSavedState {

        int progress;
        int oldProgress;

        public SavedState(Parcel source) {
            super(source);
            progress = source.readInt();
            oldProgress = source.readInt();
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            super.writeToParcel(dest, flags);
            dest.writeInt(progress);
            dest.writeInt(oldProgress);
        }

        public SavedState(Parcelable superState) {
            super(superState);
        }

        public static final Parcelable.Creator<SavedState> CREATOR =
                new Parcelable.Creator<SavedState>() {

            public SavedState createFromParcel(Parcel in) {
                return new SavedState(in);
            }

            public SavedState[] newArray(int size) {
                return new SavedState[size];
            }
        };
    }
}

